<template>
  <div class="addGood">
    <el-steps :active="active" finish-status="success" simple>
      <el-step title="步骤 1" icon="el-icon-edit"></el-step>
      <el-step title="步骤 2" icon="el-icon-upload"></el-step>
      <el-step title="步骤 3" icon="el-icon-picture"></el-step>
    </el-steps>
    <!-- 上传表单 -->
    <el-form :model="form" ref="GoodAdd" :rules="rules">
      <div v-if="active==1" class="stepNav">
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input v-model="form.price"></el-input>
        </el-form-item>
        <el-form-item label="是否上架" prop="state">
          <el-switch v-model="form.state" :active-value="1" :inactive-value="0">
          </el-switch>
        </el-form-item>
      </div>
      <div v-if="active==2" class="stepNav">
        <el-form-item label="商品描述">
          <el-input type="textarea" v-model="form.desc"></el-input>
        </el-form-item>
        <el-form-item label="商品分类" prop="cateId">
          <el-select placeholder="请选择" v-model="form.cateId">
            <el-option v-for="item in cate" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </div>
      <div v-if="active==3" class="stepNav active3">
        <el-upload class="upload-demo" drag action="/api/updataImg" multiple :on-success="onSuccess"
          :before-upload="beforeUpload" :on-remove="removeUpload">
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em>
          </div>
          <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
        <img :src="form.img" alt="" height="180">
      </div>
      <div class="bottButton">
        <el-button style="margin-top: 12px;" @click="next(form)" v-if="active!==3">下一步</el-button>
        <el-button style="margin-top: 12px;" v-else @click="subAddGood">提交</el-button>
        <el-button style="margin-top: 12px;" @click="back">返回</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { goodCate, addGood, updataGood } from '@/api'
export default {
  data() {
    // 下拉列表的规则
    const validator1 = (rule, value, callback) => {
      if (value !== -1) {
        return callback()
      } else {
        return callback(new Error('必须选择一个分类'))
      }
    }
    return {
      active: 1,
      form: {
        name: '',
        price: '',
        desc: '',
        state: 1,
        cateId: -1,
        img: ''
      },
      // 获取全部列表
      cate: [],
      // 表单验证规则
      rules: {
        name: [
          {
            required: true,
            // 验证不通过时，错误提示文案
            message: '当前字段是必填的'
          }
        ],
        price: [
          {
            required: 'number',
            // 验证不通过时，错误提示文案
            message: '当前字段是必填的'
          }
        ],
        cateId: [
          {
            required: true,
            message: '分类必须选择'
          },
          {
            validator: validator1
          }
        ]
      }
    }
  },

  methods: {
    next() {
      this.$refs.GoodAdd.validate((valid) => {
        if (valid) {
          // 验证通过
          if (this.active++ > 2) this.active = 0
        } else {
          // 验证不通过
          this.$message({
            type: 'error',
            message: '表单验证不通过'
          })
        }
      })
    },
    back() {
      if (this.active == 1) {
        this.$router.push('/goodList')
      } else if (this.active > 1) this.active--
    },
    // 上传图片之前
    beforeUpload(file) {
      // 上传文件之前  限制上传文件的体积
      if (file.size > 1024 * 1024 * 6) {
        this.$message({
          type: 'error',
          message: '请上传小于6M体积的图标'
        })
        return false
      } else {
        return true
      }
    },
    // 上传图片成功
    onSuccess(res) {
      this.$message({
        type: 'success',
        message: '图片上传成功'
      })
      this.form.img = res.img
    },
    // 移除上传图片后
    removeUpload() {
      this.form.img = ''
    },
    // 获取全部的商品分类。并那到数据
    goodCate() {
      goodCate().then(res => {
        if (res.data.code == 200) {
          this.cate = res.data.list
        }
      })
    },
    // 表单提交,修改商品信息
    subAddGood() {
      this.$refs.GoodAdd.validate((valid) => {
        if (valid) {
          // 验证通过
          updataGood(this.form).then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: '商品更新成功!',
                onClose: () => {
                  this.$router.replace('/goodList')
                }
              })
            }
          })
        } else {
          // 验证不通过
          this.$message({
            type: 'error',
            message: '表单验证不通过'
          })
        }
      })
    }
  },
  created() {
    this.form = { ...this.$route.params }
    this.goodCate()
  }
}
</script>

<style lang="scss" scoped>
.addGood {
  .el-form {
    width: 50%;
    margin: 0 auto;
  }

  .active3 {
    display: flex;

    margin: 0 auto;
    position: relative;

    img {
      position: absolute;
      right: 0px;
    }
  }

  .stepNav {
    padding-top: 50px;
    height: 300px;
  }

  .bottButton {
    margin: 0 auto;
    width: 50%;
  }
}
</style>
